package com.zzty.txj.util;

import com.zzty.txj.entity.Ability;
import com.zzty.txj.entity.DoDaily;
import com.zzty.txj.service.AbilityService;
import com.zzty.txj.service.DoDailyService;
import com.zzty.txj.serviceImpl.AbilityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;


import java.math.BigDecimal;
import javax.annotation.PostConstruct;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Component
public class ScheduledWork {

    @Autowired
    DoDailyService doDailyService;

    @Autowired
    AbilityService abilityService;

    @Scheduled(cron = "0 0 22 ? * MON,WED,FRI,SAT,SUN")
//    @Scheduled(cron = "0/30 * * ? * MON,WED,FRI,SAT,SUN") //周一三五六日 每30秒定时任务运行一次
        public void updateAbility() throws Exception {
        System.out.println("1111111111111");
        List<Ability> abilities = abilityService.getAbility();
        if (abilities == null || abilities.isEmpty()) {
            return;
        } else {
            for (Ability ability : abilities) {
                Integer userId = ability.getUserId();
                System.out.println("userId: "+userId);
                List<DoDaily> records = doDailyService.getDoDailyByUserId(userId);
                double resultCount = 0;
                if (records == null || records.isEmpty()) {
                    continue;
                } else {
                    double record = records.size();
                    for (DoDaily d : records) {
                        if (d.getResult() == -1) {
                            resultCount++;
                        }
                    }
                    double pAbility = new BigDecimal((float)resultCount / record).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    double cAbility = new BigDecimal((float)resultCount / record).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    double performAbility = pAbility*100;
                    double crediAbility = cAbility*100;
//                    String pAbility = Integer.parseInt(new DecimalFormat("0").format(performAbility * 100)) + "%";//百分比没有小数点
//                    String cAbility = Integer.parseInt(new DecimalFormat("0").format(crediAbility * 100)) + "%";//百分比没有小数点
                    ability.setPerformAbility(performAbility);
                    ability.setCredibility(crediAbility);
                    ability.setStudyAbility(100.0);
                    ability.setStudyAbility(100.0);
                    ability.setLaziness(100.0);
                    abilityService.updateAbility(ability);
                }
            }
        }
    }


}
